//
//  FKSharePhotoCell.m
//  WanTao
//
//  Created by Macbook Pro on 2019/4/17.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKSharePhotoCell.h"

static const CGFloat bottomViewHeight = 160.0;

@interface FKSharePhotoCell ()

@property (nonatomic, weak) UIView *container; // 整体容器
@property (nonatomic, weak) UIView *bottomView; // 底部容器
@property (nonatomic, weak) UIImageView *photoView; // top 图片
@property (nonatomic, weak) UIImageView *qrcodeView;  // 二维码
@property (nonatomic, weak) UILabel *nicknameLabel; // 标题
@property (nonatomic, weak) UIImageView *avatarView; //

// 新增的控件
// 抢购时间 & 使用时间
@property (nonatomic, strong) UILabel *saleTimeLabel;
@property (nonatomic, strong) UILabel *activeTimeLabel;

// 非vip 的控件



@end

@implementation FKSharePhotoCell

- (void)setupSubviews {
    [super setupSubviews];
    self.contentView.backgroundColor = [UIColor clearColor];
}

- (void)setVipView {
    [self.contentView fk_removeAllSubviews];
    UIView *container = [[UIView alloc] init];
    [self.contentView addSubview:container];
    self.container = container;
    container.backgroundColor = [UIColor whiteColor];
    [container fk_viewCornerRadius:5 borderWidth:0 borderColor:nil];
    
    UIImageView *photoView = [[UIImageView alloc] init];
    [container addSubview:photoView];
    self.photoView = photoView;
    photoView.contentMode = UIViewContentModeScaleAspectFill;
    
    UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"new_logo"]];
    [container addSubview:iconView];
    
    UIView *bottomView = [[UIView alloc] init];
    self.bottomView = bottomView;
    bottomView.clipsToBounds = true;
    [container addSubview:bottomView];
    
    UIImageView *qrcodeView = [[UIImageView alloc] init];
    [bottomView addSubview:qrcodeView];
    self.qrcodeView = qrcodeView;
    qrcodeView.backgroundColor = fkBgColor;
    
    UILabel *nicknameLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor333333];
    [bottomView addSubview:nicknameLabel];
    self.nicknameLabel = nicknameLabel;
    
    UILabel *descLabel = [UILabel fk_labelWithFont:fk_adjustFont(10) textColor:fkColor666666 text:@"我为你推荐"];
    [bottomView addSubview:descLabel];
    
    UIImageView *tipView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"easyBuy2"]];
    [bottomView addSubview:tipView];
    
    UIImageView *avatarView = [[UIImageView alloc] init];
    [bottomView addSubview:avatarView];
    self.avatarView = avatarView;
    [avatarView fk_viewCornerRadius:fk_adjustW(48) borderWidth:0 borderColor:nil];
    
    // bottomView 新增 控件
    UIImageView *iconView2= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]];
    [bottomView addSubview:iconView2];
    // sale time
    CGSize leadingLabelSize = CGSizeMake(fk_adjustW(57), fk_adjustW(15));
    UILabel *stLabel = [UILabel fk_labelWithFont:fkFont12 textColor:fkColor(255, 255, 255) text:@"抢购时间"];
    stLabel.textAlignment = NSTextAlignmentCenter;
    stLabel.layer.masksToBounds = true;
    stLabel.layer.cornerRadius = 4;
    stLabel.backgroundColor = [UIColor fk_colorWithHexString:@"#DB2C5C"];
    [bottomView addSubview:stLabel];
    self.saleTimeLabel = [UILabel fk_labelWithFont:fkFont12 textColor:fkColor666666];
    [bottomView addSubview:self.saleTimeLabel];
    // active
    UILabel *atLabel = [UILabel fk_labelWithFont:fkFont12 textColor:fkColor(255, 255, 255) text:@"使用时间"];
    atLabel.textAlignment = NSTextAlignmentCenter;
    atLabel.layer.masksToBounds = true;
    atLabel.layer.cornerRadius = 4;
    atLabel.backgroundColor = [UIColor fk_colorWithHexString:@"#DB2C5C"];
    [bottomView addSubview:atLabel];
    self.activeTimeLabel = [UILabel fk_labelWithFont:fkFont12 textColor:fkColor666666];
    [bottomView addSubview:self.activeTimeLabel];
    
    [container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.centerX.offset(0);
        make.height.offset(fkScreenH - fk_adjustW(120));
        make.width.offset(fkScreenW - fk_adjustW(20));
        //        make.width.offset(fkScreenW);
    }];
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.top.offset(10);
        make.width.height.offset(fk_adjustW(40));
    }];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.offset(0);
        make.height.offset(fk_adjustW(bottomViewHeight));
    }];
    [photoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.bottom.equalTo(bottomView.mas_top);
    }];
    [qrcodeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(fk_adjustW(18));
        make.height.width.offset(fk_adjustW(90));
        make.bottom.offset(-8);
    }];
    [nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(qrcodeView.mas_right).offset(30);
        make.top.equalTo(qrcodeView).offset(8);
    }];
    [tipView mas_makeConstraints:^(MASConstraintMaker *make) {
        //        make.left.equalTo(nicknameLabel);
        make.centerX.equalTo(bottomView);
        make.bottom.equalTo(bottomView.mas_bottom).offset(-20);
        make.width.offset(fk_adjustW(80));
        make.height.offset(fk_adjustW(35));
    }];
    [descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nicknameLabel);
        make.top.equalTo(nicknameLabel.mas_bottom).offset(4);
    }];
    [avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(fk_adjustW(96));
        //        make.centerY.equalTo(qrcodeView);
        make.right.offset(-16);
        make.bottom.offset(-6);
    }];
    // 新增的控件
    [iconView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(fk_adjustW(44), fk_adjustW(44)));
        make.right.mas_offset(-10);
        make.top.mas_offset(6);
    }];
    // sale time
    [stLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(leadingLabelSize);
        make.top.mas_offset(10);
        make.left.mas_offset(4);
    }];
    NSMutableAttributedString *saletime = [[NSMutableAttributedString alloc] initWithString:self.data[@"saleTime"]];
    [saletime addAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"EuphemiaUCAS" size:12]} range:NSMakeRange(0, [self.data[@"saleTime"] length])];
    for (int i = 0; i < [self getRangeStr:saletime.string findText:@"年" ].count; i++) {
        NSInteger location = [[self getRangeStr:saletime.string findText:@"年" ][i] integerValue];
        [saletime addAttributes:@{NSForegroundColorAttributeName:[UIColor fk_colorWithHexString:@"#DB2C5C"]} range:NSMakeRange(location + 1, 2)];
    }
    for (int i = 0; i < [self getRangeStr:saletime.string findText:@"月" ].count; i++) {
        NSInteger location = [[self getRangeStr:saletime.string findText:@"月" ][i] integerValue];
        [saletime addAttributes:@{NSForegroundColorAttributeName:[UIColor fk_colorWithHexString:@"#DB2C5C"]} range:NSMakeRange(location + 1, 2)];
    }
    self.saleTimeLabel.attributedText = saletime;
    [self.saleTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(stLabel);
        make.top.mas_equalTo(stLabel);
        make.left.mas_equalTo(stLabel.mas_right).offset(10);
        make.right.mas_equalTo(iconView2.mas_left).offset(-10);
    }];
    // active time
    [atLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(leadingLabelSize);
        make.top.mas_equalTo(stLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(stLabel);
    }];
    NSMutableAttributedString *activetime = [[NSMutableAttributedString alloc] initWithString:self.data[@"activeTime"]];
    [activetime addAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"EuphemiaUCAS" size:12]} range:NSMakeRange(0, [self.data[@"activeTime"] length])];
    for (int i = 0; i < [self getRangeStr:activetime.string findText:@"年" ].count; i++) {
        NSInteger location = [[self getRangeStr:activetime.string findText:@"年" ][i] integerValue];
        [activetime addAttributes:@{NSForegroundColorAttributeName:[UIColor fk_colorWithHexString:@"#DB2C5C"]} range:NSMakeRange(location + 1, 2)];
    }
    for (int i = 0; i < [self getRangeStr:activetime.string findText:@"月" ].count; i++) {
        NSInteger location = [[self getRangeStr:activetime.string findText:@"月" ][i] integerValue];
        [activetime addAttributes:@{NSForegroundColorAttributeName:[UIColor fk_colorWithHexString:@"#DB2C5C"]} range:NSMakeRange(location + 1, 2)];
    }
    self.activeTimeLabel.attributedText = activetime;
    [self.activeTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(atLabel);
        make.top.mas_equalTo(atLabel);
        make.left.mas_equalTo(atLabel.mas_right).offset(10);
        make.right.mas_equalTo(iconView2.mas_left).offset(-10);
    }];
}

- (void)setDefaultView {
    [self.contentView fk_removeAllSubviews];
    UIView *container = [[UIView alloc] init];
    [self.contentView addSubview:container];
    self.container = container;
    container.backgroundColor = [UIColor whiteColor];
    [container fk_viewCornerRadius:5 borderWidth:0 borderColor:nil];
    
    UIImageView *photoView = [[UIImageView alloc] init];
    [container addSubview:photoView];
    self.photoView = photoView;
    photoView.contentMode = UIViewContentModeScaleAspectFill;
    
    UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"new_logo"]];
    [container addSubview:iconView];
    
    UIView *bottomView = [[UIView alloc] init];
    self.bottomView = bottomView;
    bottomView.clipsToBounds = true;
    [container addSubview:bottomView];
    
    UIImageView *qrcodeView = [[UIImageView alloc] init];
    [bottomView addSubview:qrcodeView];
    self.qrcodeView = qrcodeView;
    qrcodeView.backgroundColor = fkBgColor;
    
    // bottomView 新增 控件
    UIImageView *iconView2= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]];
    [bottomView addSubview:iconView2];
    // sale time
    CGSize leadingLabelSize = CGSizeMake(fk_adjustW(57), fk_adjustW(15));
    UILabel *stLabel = [UILabel fk_labelWithFont:fkFont12 textColor:fkColor(255, 255, 255) text:@"抢购时间"];
    stLabel.textAlignment = NSTextAlignmentCenter;
    stLabel.layer.masksToBounds = true;
    stLabel.layer.cornerRadius = 4;
    stLabel.backgroundColor = [UIColor fk_colorWithHexString:@"#DB2C5C"];
    [bottomView addSubview:stLabel];
    self.saleTimeLabel = [UILabel fk_labelWithFont:fkFont12 textColor:fkColor666666];
    [bottomView addSubview:self.saleTimeLabel];
    // active
    UILabel *atLabel = [UILabel fk_labelWithFont:fkFont12 textColor:fkColor(255, 255, 255) text:@"使用时间"];
    atLabel.textAlignment = NSTextAlignmentCenter;
    atLabel.layer.masksToBounds = true;
    atLabel.layer.cornerRadius = 4;
    atLabel.backgroundColor = [UIColor fk_colorWithHexString:@"#DB2C5C"];
    [bottomView addSubview:atLabel];
    self.activeTimeLabel = [UILabel fk_labelWithFont:fkFont12 textColor:fkColor666666];
    [bottomView addSubview:self.activeTimeLabel];
    
    UIImageView *defaultImg = [[UIImageView alloc] init];
    [defaultImg setImage:[UIImage imageNamed:@"default_bottom_img"]];
    //        defaultImg.backgroundColor = UIColor.orangeColor;
    [bottomView addSubview:defaultImg];
    
    [container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.centerX.offset(0);
        make.height.offset(fkScreenH - fk_adjustW(120));
        make.width.offset(fkScreenW - fk_adjustW(20));
        //        make.width.offset(fkScreenW);
    }];
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.top.offset(10);
        make.width.height.offset(fk_adjustW(40));
    }];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.offset(0);
        make.height.offset(fk_adjustW(bottomViewHeight));
    }];
    [photoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.bottom.equalTo(bottomView.mas_top);
    }];
    [qrcodeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(fk_adjustW(18));
        make.height.width.offset(fk_adjustW(90));
        make.bottom.offset(-8);
    }];
    
    // 新增的控件
    [iconView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(fk_adjustW(44), fk_adjustW(44)));
        make.right.mas_offset(-10);
        make.top.mas_offset(6);
    }];
    // sale time
    [stLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(leadingLabelSize);
        make.top.mas_offset(10);
        make.left.mas_offset(4);
    }];
    NSMutableAttributedString *saletime = [[NSMutableAttributedString alloc] initWithString:self.data[@"saleTime"]];
    [saletime addAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"EuphemiaUCAS" size:12]} range:NSMakeRange(0, [self.data[@"saleTime"] length])];
    for (int i = 0; i < [self getRangeStr:saletime.string findText:@"年" ].count; i++) {
        NSInteger location = [[self getRangeStr:saletime.string findText:@"年" ][i] integerValue];
        [saletime addAttributes:@{NSForegroundColorAttributeName:[UIColor fk_colorWithHexString:@"#DB2C5C"]} range:NSMakeRange(location + 1, 2)];
    }
    for (int i = 0; i < [self getRangeStr:saletime.string findText:@"月" ].count; i++) {
        NSInteger location = [[self getRangeStr:saletime.string findText:@"月" ][i] integerValue];
        [saletime addAttributes:@{NSForegroundColorAttributeName:[UIColor fk_colorWithHexString:@"#DB2C5C"]} range:NSMakeRange(location + 1, 2)];
    }
    self.saleTimeLabel.attributedText = saletime;
    [self.saleTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(stLabel);
        make.top.mas_equalTo(stLabel);
        make.left.mas_equalTo(stLabel.mas_right).offset(10);
        make.right.mas_equalTo(iconView2.mas_left).offset(-10);
    }];
    // active time
    [atLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(leadingLabelSize);
        make.top.mas_equalTo(stLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(stLabel);
    }];
    NSMutableAttributedString *activetime = [[NSMutableAttributedString alloc] initWithString:self.data[@"activeTime"]];
    [activetime addAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"EuphemiaUCAS" size:12]} range:NSMakeRange(0, [self.data[@"activeTime"] length])];
    for (int i = 0; i < [self getRangeStr:activetime.string findText:@"年" ].count; i++) {
        NSInteger location = [[self getRangeStr:activetime.string findText:@"年" ][i] integerValue];
        [activetime addAttributes:@{NSForegroundColorAttributeName:[UIColor fk_colorWithHexString:@"#DB2C5C"]} range:NSMakeRange(location + 1, 2)];
    }
    for (int i = 0; i < [self getRangeStr:activetime.string findText:@"月" ].count; i++) {
        NSInteger location = [[self getRangeStr:activetime.string findText:@"月" ][i] integerValue];
        [activetime addAttributes:@{NSForegroundColorAttributeName:[UIColor fk_colorWithHexString:@"#DB2C5C"]} range:NSMakeRange(location + 1, 2)];
    }
    self.activeTimeLabel.attributedText = activetime;
    [self.activeTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(atLabel);
        make.top.mas_equalTo(atLabel);
        make.left.mas_equalTo(atLabel.mas_right).offset(10);
        make.right.mas_equalTo(iconView2.mas_left).offset(-10);
    }];
    
    [defaultImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(fk_adjustW(214), fk_adjustW(98)));
        make.bottom.mas_equalTo(-6);
        make.right.mas_equalTo(-24);
    }];
}

- (void)setData:(id)data
{
    [super setData:data];
    if ([data isKindOfClass:[NSDictionary class]]) {
        if ([self.data[@"level"] intValue] == 10) {// 有vip 和非 vip的区别
            [self setDefaultView];
        } else {
            [self setVipView];
        }
        fkWeakSelf(self);
        [self.photoView sd_setImageWithURL:[NSURL URLWithString:data[@"url"]] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            CGFloat containerRatio = (fkScreenW - fk_adjustW(20)) / (fkScreenH - fk_adjustW(80) - fk_adjustW(bottomViewHeight));
            CGFloat imageRatio = image.size.width / image.size.height;
            FKLog(@"%f",containerRatio);
            FKLog(@"%f",imageRatio);
            if (containerRatio < imageRatio) {
                if (self.tag != 0) {
                    [weakself.container mas_updateConstraints:^(MASConstraintMaker *make) {
                        CGFloat containerH = (fkScreenW - fk_adjustW(20)) / imageRatio;
                        make.height.offset(containerH);
                    }];
                    [weakself.bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
                        make.height.offset(0);
                    }];
                } else {
                    [weakself.container mas_updateConstraints:^(MASConstraintMaker *make) {
                        CGFloat containerH = (fkScreenW - fk_adjustW(20)) / imageRatio + fk_adjustW(bottomViewHeight);
                        make.height.offset(containerH);
                    }];
                }
            } else {
                [weakself.container mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.offset(fkScreenH - fk_adjustW(80));
                }];
            }
        }];
        NSString *url = data[@"qrcode"];
//        NSString *url = @"www.baidu.com";
        self.qrcodeView.image = [self addImage:[UIImage imageNamed:@"AppIcon"] toImage:[UIImage fk_imageGenerateQRCode:url width:200 height:200]];
//        self.nicknameLabel.text = data[@"name"];
        NSString *name = [NSString stringWithFormat:@"我是%@",data[@"name"]];
        NSMutableAttributedString *nickname = [[NSMutableAttributedString alloc] initWithString:name];
        // red #DB2C5C
        [nickname setAttributes:@{NSFontAttributeName:fkFont12} range:NSMakeRange(0, 2)];
        [nickname setAttributes:@{NSForegroundColorAttributeName:[UIColor fk_colorWithHexString:@"#777777"]} range:NSMakeRange(0, 2)];
        [nickname setAttributes:@{NSFontAttributeName:fkFont16} range:NSMakeRange(2, nickname.length - 2)];
        [nickname setAttributes:@{NSForegroundColorAttributeName:[UIColor fk_colorWithHexString:@"#DB2C5C"]} range:NSMakeRange(2, nickname.length - 2)];

        self.nicknameLabel.attributedText = nickname;
        [self.avatarView sd_setImageWithURL:[NSURL URLWithString:data[@"avatar"]] placeholderImage:nil];
    }
}

- (UIImage *)addImage:(UIImage *)firstImage toImage:(UIImage *)secondImage {
    CGFloat firstSizeW = fk_adjustW(30);
    UIGraphicsBeginImageContext(secondImage.size);
    [secondImage drawInRect:CGRectMake(0, 0, secondImage.size.height, secondImage.size.width)];
    [firstImage drawInRect:CGRectMake((secondImage.size.height - firstSizeW)/2, (secondImage.size.width - firstSizeW)/2, firstSizeW, firstSizeW)];
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return result;
}


@end
